#include "dicom/file_object.h"

#include <json/json.h>
#include <gwbase/base/random.h>

namespace dicom {

static Random g_fid_rand;
static const int kFileIdLength = 20;
static const char kFileIdAlphabet[] = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'G', 'K', 'L', 'M',
    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', };

std::string GenFileId() {
  std::string id;
  for (int i = 0; i < kFileIdLength; ++i) {
    id.push_back(kFileIdAlphabet[g_fid_rand.Uniform(sizeof(kFileIdAlphabet))]);
  }
  return id;
}

FileObject::FileObject()
    : type("file"),
      size(0),
      star(false),
      is_delete(false),
      svgPageCount(0),
      metaV(0),
      version(0),
      newDoc(false),
      isSepModified(false) {}

void FileObject::ToJson(Json::Value& v) const {
  v["type"] = type;
  v["id"] = local_id;
  v["name"] = name;
  v["createTime"] = createTime.ToISO8601();
  v["modifiedTime"] = modifiedTime.ToISO8601();
  v["creator"] = creator;
  v["parentId"] = parentId;
  v["size"] = static_cast<const Json::Int64>(size);
  v["star"] = star;
  v["is_delete"] = is_delete;
  v["svgPageCount"] = static_cast<const Json::Int>(svgPageCount);
  v["metaV"] = metaV;
  v["digest"] = digest;
  v["version"] = version;
  v["newDoc"] = newDoc;
  v["allParentId"] = allParentId;
  v["isSepModified"] = isSepModified;
  v["digest"] = digest;
}

std::string FileObject::ToJson() const {
  Json::Value v;
  ToJson(v);

  return v.toStyledString();
}

/*
bool FileObject::Parse(const std::string& s, FileObject* fo) {
  Json::Value v;
  Json::Reader r;
  if (r.parse(s, v, false) == false)
    return false;

  fo->type = v["type"].asString();
  fo->id = v["id"].asString();
  fo->name = v["name"].asString();
  fo->createTime = Timestamp::FromISO8601(v["createTime"].asString());
  fo->modifiedTime = Timestamp::FromISO8601(v["modifiedTime"].asString());
  fo->creator = v["creator"].asString();
  fo->parentId = v["parentId"].asString();
  fo->size = v["size"].asUInt64();
  fo->star = v["star"].asInt();
  fo->is_delete = v["is_delete"].asInt();
  fo->svgPageCount = v["svgPageCount"].asInt();
  fo->metaV = v["metaV"].asInt();
  fo->digest = v["metaV"].asString();
  fo->version = v["version"].asInt();
  fo->newDoc = v["newDoc"].asInt();
  fo->allParentId = v["allParentId"].asString();
  fo->isSepModified = v["isSepModified"].asInt();

  return true;
}
*/

DirObject::DirObject()
    : type("folder"),
      star(false),
      is_delete(false),
      share(false),
      fileCount(0) {}

std::string DirObject::ToJson() const {
  Json::Value v;
  ToJson(v);

  return v.toStyledString();
}

void DirObject::ToJson(Json::Value& v) const {
  v["type"] = type;
  v["id"] = local_id;
  v["name"] = name;
  v["createTime"] = createTime.ToISO8601();
  v["modifiedTime"] = modifiedTime.ToISO8601();
  v["creator"] = creator;
  v["parentId"] = parentId;
  v["star"] = star;
  v["allParentId"] = allParentId;
  v["fileCount"] = fileCount;
  v["size"] = 4096;
}

/*
bool DirObject::Parse(const std::string& s, DirObject* d) {
  Json::Value v;
  Json::Reader r;
  if (r.parse(s, v, false) == false)
    return false;

  d->type = v["type"].asString();
  d->id = v["id"].asString();
  d->name = v["name"].asString();
  d->createTime = Timestamp::FromISO8601(v["createTime"].asString());
  d->modifiedTime = Timestamp::FromISO8601(v["modifiedTime"].asString());
  d->creator = v["creator"].asString();
  d->parentId = v["parentId"].asString();
  d->star = v["parentId"].asInt();
  d->allParentId = v["allParentId"].asString();
  d->fileCount = v["fileCount"].asInt();

  return true;
}
*/
}
